package dtn.news;

import dtn.supports.Constant;
import dtn.supports.PreferenceConnector;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TableLayout;
import android.widget.TextView;

public class PageSetting extends Activity {

	private SeekBar sbrNews;
	private SeekBar sbrCareer;
	private SeekBar sbrScience;
	private SeekBar sbrUpdateTime;
	private RadioButton rbtnAuto;
	private RadioButton rbtnManual;
	private Button btnSave;
	private Button btnDefault;
	private RadioGroup rdGroup;
	private TextView txtAmountNews;
	private TextView txtAmountCareer;
	private TextView txtAmountScience;
	private TextView txtAmountUpdateTime;
	private TableLayout tableTime;
	private boolean isDefault = true;
	private RadioButton rbAutoUpdate;
	private RadioButton rbManual;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		
		Init();

	}

	// ==================KHỐI HÀM SỰ KIỆN====================
	//Seek bar event
	private OnSeekBarChangeListener sbtnChangeListener = new OnSeekBarChangeListener() {

		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			
			if (seekBar.equals(sbrUpdateTime)) {
				int amount = (progress * Constant.MAX_TIME_AUTO_UPDATE / 100)
						+ Constant.MIN_TIME_AUTO_UPDATE;
				txtAmountUpdateTime.setText(amount + " phút");
			} else {
				int amount = (progress * Constant.MAX_NEWS / 100)
						+ Constant.MIN_NEWS;
				if (seekBar.equals(sbrNews)) {
					txtAmountNews.setText(amount + " tin");
				} else if (seekBar.equals(sbrCareer)) {
					txtAmountCareer.setText(amount + " tin");
				} else if (seekBar.equals(sbrScience)) {
					txtAmountScience.setText(amount + " tin");
				}
			}

		}

		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub

		}

	};
	
	//Group radio event
	private OnCheckedChangeListener rdGroupCheckedChangeListener = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup rdg, int checkId) {
			if(checkId == R.id.rbtn_auto) {
				tableTime.setVisibility(View.VISIBLE);
			}
			else {
				//rbtnManual.setChecked(true);
				tableTime.setVisibility(View.GONE);
			}
		}
		
	};
	
	//Button mặc định
	private OnClickListener btnDefaultClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			int amount = Constant.DEFAULT_NEWS;
			int progress = amount  * 100 / Constant.MAX_NEWS;
			//txtAmountNews.setText(progress + " tin");
			//txtAmountCareer.setText(amount + " tin");
			//txtAmountScience.setText(amount + " tin");
			sbrNews.setProgress(progress);
			sbrCareer.setProgress(progress);
			sbrScience.setProgress(progress);
			
			rbtnManual = (RadioButton) findViewById(R.id.rbtn_manual);
			rbtnManual.setChecked(true);
			
		}
	};
	
	private OnClickListener btnSaveClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			save(v);
			finish();
		}
	};
	
	
	//===== KHỐI HÀM XỬ LÝ============
	private void Init() {
		//Layout 
		tableTime = (TableLayout) findViewById(R.id.tl_auto_update);

		// Seek bar
		sbrNews = (SeekBar) findViewById(R.id.sbr_news);
		sbrNews.setOnSeekBarChangeListener(sbtnChangeListener);
		sbrCareer = (SeekBar) findViewById(R.id.sbr_career);
		sbrCareer.setOnSeekBarChangeListener(sbtnChangeListener);
		sbrScience = (SeekBar) findViewById(R.id.sbr_science);
		sbrScience.setOnSeekBarChangeListener(sbtnChangeListener);
		sbrUpdateTime = (SeekBar) findViewById(R.id.sbr_update_time);
		sbrUpdateTime.setOnSeekBarChangeListener(sbtnChangeListener);

		// Button
		rdGroup = (RadioGroup) findViewById(R.id.grd_check);
		rdGroup.setOnCheckedChangeListener(rdGroupCheckedChangeListener);
		rbAutoUpdate = (RadioButton) findViewById(R.id.rbtn_auto);
		rbManual = (RadioButton) findViewById(R.id.rbtn_manual);
		btnSave = (Button) findViewById(R.id.btn_save);
		btnSave.setOnClickListener(btnSaveClickListener);
		btnDefault = (Button) findViewById(R.id.btn_default);
		btnDefault.setOnClickListener(btnDefaultClickListener);

		// TextView
		txtAmountNews = (TextView) findViewById(R.id.txt_amount_news);
		txtAmountCareer = (TextView) findViewById(R.id.txt_amount_career);
		txtAmountScience = (TextView) findViewById(R.id.txt_amount_science);
		txtAmountUpdateTime = (TextView) findViewById(R.id.txt_update_time);
		
		getData();
	}
	
	//Luu tuy chinh
	private void save(View view) {
		int amountNews = sbrNews.getProgress();
		int amountCareer = sbrCareer.getProgress();
		int amountSciecen = sbrScience.getProgress();
		boolean autoUpdate = rbAutoUpdate.isChecked();
		int timeUpdate = sbrUpdateTime.getProgress();
		
		PreferenceConnector.writeInteger(this , Constant.KEY_AMOUNT_NEWS, amountNews);
		PreferenceConnector.writeInteger(this , Constant.KEY_AMOUNT_CAREER, amountCareer);
		PreferenceConnector.writeInteger(this , Constant.KEY_AMOUNT_SCIENCE, amountSciecen);
		PreferenceConnector.writeBoolean(this , Constant.KEY_AUTO_UPDATE, autoUpdate);
		PreferenceConnector.writeInteger(this , Constant.KEY_TIME_UPDATE, timeUpdate);
	}
	
	//Lay Du Lieu Tuy Chinh
	private void getData() {
		int defaultValue = Constant.DEFAULT_NEWS + Constant.MIN_NEWS;
		sbrNews.setProgress(PreferenceConnector.readInteger(this , Constant.KEY_AMOUNT_NEWS, defaultValue));
		sbrCareer.setProgress(PreferenceConnector.readInteger(this , Constant.KEY_AMOUNT_CAREER, defaultValue));
		sbrScience.setProgress(PreferenceConnector.readInteger(this , Constant.KEY_AMOUNT_SCIENCE, defaultValue));
		rbAutoUpdate.setChecked(PreferenceConnector.readBoolean(this , Constant.KEY_AUTO_UPDATE, false));
		sbrUpdateTime.setProgress(PreferenceConnector.readInteger(this , Constant.KEY_TIME_UPDATE, Constant.MIN_TIME_AUTO_UPDATE));
	}
}
